package com.fanyue.modules.wzb.service.impl;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.fanyue.core.base.BaseServiceImpl;
import com.fanyue.core.cache.CoolCache;
import com.fanyue.core.exception.CoolPreconditions;
import com.fanyue.core.request.R;
import com.fanyue.core.util.CoolSecurityUtil;
import com.fanyue.core.util.DatabaseDialectUtils;
import com.fanyue.modules.base.entity.sys.BaseSysParamEntity;
import com.fanyue.modules.base.entity.sys.BaseSysUserEntity;
import com.fanyue.modules.base.service.sys.BaseSysParamService;
import com.fanyue.modules.base.service.sys.BaseSysUserService;
import com.fanyue.modules.wzb.entity.*;
import com.fanyue.modules.wzb.mapper.CargoMapper;
import com.fanyue.modules.wzb.mapper.ClothingInfoMapper;
import com.fanyue.modules.wzb.service.*;
import com.fanyue.modules.wzb.vo.ClothingBatchVO;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.tangzc.autotable.core.constants.DatabaseDialect;
import io.netty.util.internal.StringUtil;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.fanyue.modules.base.entity.sys.table.BaseSysDepartmentEntityTableDef.BASE_SYS_DEPARTMENT_ENTITY;
import static com.fanyue.modules.wzb.entity.table.AppUserEntityTableDef.APP_USER_ENTITY;
import static com.fanyue.modules.wzb.entity.table.CargoEntityTableDef.CARGO_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingBatchEntityTableDef.CLOTHING_BATCH_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingInfoEntityTableDef.CLOTHING_INFO_ENTITY;
import static com.fanyue.modules.wzb.entity.table.ClothingSpecEntityTableDef.CLOTHING_SPEC_ENTITY;


/**
 * 系统用户
 */
@Service
@RequiredArgsConstructor
public class ClothingInfoServiceImpl extends BaseServiceImpl<ClothingInfoMapper, ClothingInfoEntity> implements ClothingInfoService {

    final private BaseSysUserService baseSysUserService;
    final private AppUserService appUserService;
    final private CargoMapper cargoMapper;
    final private ClothingRecordService clothingRecordService;
    final private BaseSysParamService sysParamService;
    final private ClothingBatchItemService clothingBatchItemService;

    @Override
    public Object page(JSONObject requestParams, Page<ClothingInfoEntity> page, QueryWrapper qw) {
        String keyWord = requestParams.getStr("keyWord");
        Integer isPrivate = requestParams.getInt("isPrivate");
        Integer category = requestParams.getInt("category");
        Integer season = requestParams.getInt("season");
        Integer status = requestParams.getInt("status");
        Integer specId = requestParams.getInt("specId");

        /*Long[] departmentIds = requestParams.get("departmentIds", Long[].class);
        JSONObject tokenInfo = CoolSecurityUtil.getAdminUserInfo(requestParams);
        // 用户的部门权限
        Long[] permsDepartmentArr = coolCache.get("admin:department:" + tokenInfo.get("userId"),
                Long[].class);*/
        String databaseDialect = DatabaseDialectUtils.getDatabaseDialect();
        if (databaseDialect.equals(DatabaseDialect.PostgreSQL)) {
            // 兼容postgresql
        } else {
            qw.select(CLOTHING_INFO_ENTITY.ALL_COLUMNS,
                    CARGO_ENTITY.POSITION.as("cargoPosition"),
                    APP_USER_ENTITY.as("opUser").NAME.as("opUserName"),
                    APP_USER_ENTITY.as("prUser").NAME.as("prUserName"),
                    BASE_SYS_DEPARTMENT_ENTITY.NAME.as("departmentName"));
        }
        qw.from(CLOTHING_INFO_ENTITY).leftJoin(CARGO_ENTITY)
                .on(CLOTHING_INFO_ENTITY.CARGO_ID.eq(CARGO_ENTITY.ID))
                .leftJoin(APP_USER_ENTITY).as("opUser")
                .on(CLOTHING_INFO_ENTITY.OP_USER_ID.eq(APP_USER_ENTITY.as("opUser").ID))
                .leftJoin(APP_USER_ENTITY).as("prUser")
                .on(CLOTHING_INFO_ENTITY.PR_USER_ID.eq(APP_USER_ENTITY.as("prUser").ID))
                .leftJoin(BASE_SYS_DEPARTMENT_ENTITY)
                .on(CLOTHING_INFO_ENTITY.DEPARTMENT_ID.eq(BASE_SYS_DEPARTMENT_ENTITY.ID));

        if (StrUtil.isNotEmpty(keyWord)) {
            qw.and(CLOTHING_INFO_ENTITY.CODE.like(keyWord)
                    .or(CLOTHING_INFO_ENTITY.AFF_CODE.like(keyWord)));
        }
        qw.and(CLOTHING_INFO_ENTITY.STATUS.eq(status, status != null));
        qw.and(CLOTHING_INFO_ENTITY.IS_PRIVATE.eq(isPrivate, isPrivate != null));
        qw.and(CLOTHING_INFO_ENTITY.SEASON.eq(season, season != null));
        qw.and(CLOTHING_INFO_ENTITY.CATEGORY.eq(category, category != null));
        qw.and(CLOTHING_INFO_ENTITY.SPEC_ID.eq(specId, specId != null));
        //qw.and(CLOTHING_INFO_ENTITY.TYPE.eq(type, StrUtil.isNotEmpty(type)));
        // 筛选部门
        /*qw.and(CLOTHING_INFO_ENTITY.DEPARTMENT_ID.in(departmentIds,
                ArrayUtil.isNotEmpty(departmentIds)));
        // 过滤部门权限
        qw.and(CLOTHING_INFO_ENTITY.DEPARTMENT_ID.in(
                permsDepartmentArr == null || permsDepartmentArr.length == 0 ? new Long[]{null}
                        : permsDepartmentArr,
                !CoolSecurityUtil.getAdminUsername().equals("admin")));*/
        if (databaseDialect.equals(DatabaseDialect.PostgreSQL)) {
            // 兼容postgresql
            qw.groupBy("base_sys_department.name");
        } else {
            qw.groupBy(CLOTHING_INFO_ENTITY.ID);
        }
        return mapper.paginate(page, qw);
    }

    @Override
    public Long add(JSONObject requestParams, ClothingInfoEntity entity) {
        Long opUserId = requestParams.getLong("opUserId");
        ClothingInfoEntity check = getOne(
                QueryWrapper.create().eq(ClothingInfoEntity::getCode, entity.getCode())
                        .or(w ->{
                            w.eq(ClothingInfoEntity::getAffCode,  entity.getCode());
                        }));
        CoolPreconditions.check(check != null, "编号已存在已存在");

        if (entity.getCategory() == 2) {
            ClothingInfoEntity check1 = getOne(
                    QueryWrapper.create().eq(ClothingInfoEntity::getCode, entity.getAffCode())
                            .or(w ->{
                                w.eq(ClothingInfoEntity::getAffCode,  entity.getAffCode());
                            }));
            CoolPreconditions.check(check1 != null, "附属编号已存在已存在");
        } else {
            entity.setAffCode("");
        }
        if (entity.getCategory() == 4 || entity.getCategory() == 3) {
            entity.setSeason(0);
        }
        super.add(requestParams, entity);
        JSONObject tokenInfo = requestParams.getJSONObject("tokenInfo");
        BaseSysUserEntity sysUserEntity = baseSysUserService.getById(tokenInfo.get("userId").toString());
        //判断状态，1-在库，更新货架状态，2-借出、3-损坏，增加记录，
        ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
        clothingRecord.setCreateTime(new Date());
        clothingRecord.setClothingId(entity.getId());
        clothingRecord.setClothingCode(entity.getCode());
        clothingRecord.setSysUserId(sysUserEntity.getId());
        clothingRecord.setSysUserName(sysUserEntity.getName());
        //clothingRecord.setDepartmentId(entity.getDepartmentId());
        if (entity.getStatus() == 1) {
            CargoEntity cargo = cargoMapper.selectOneById(entity.getCargoId());
            clothingRecord.setCargoId(cargo.getId());
            clothingRecord.setCargoPosition(cargo.getPosition());
            clothingRecord.setType(5);
            clothingRecord.setStatus(1);
            clothingRecordService.save(clothingRecord);
            //修改货架状态
            cargo.setStatus(1);
            cargoMapper.update(cargo);
        }
        if (entity.getStatus() == 2) {
            CoolPreconditions.check(opUserId == null, "请选择借出人员");
            AppUserEntity appUser = appUserService.getById(opUserId);

            clothingRecord.setAppUserId(appUser.getId());
            clothingRecord.setType(1);
            clothingRecord.setStatus(1);
            clothingRecordService.save(clothingRecord);
        }
        if (entity.getStatus() == 3) {

            clothingRecord.setType(6);
            clothingRecord.setStatus(0);
            clothingRecordService.save(clothingRecord);
        }
        return entity.getId();
    }

    @Override
    public boolean update(JSONObject requestParams, ClothingInfoEntity entity) {
        ClothingInfoEntity check = getOne(
                QueryWrapper.create()
                        .and(queryWrapper -> {
                            queryWrapper.eq(ClothingInfoEntity::getCode, entity.getCode());
                            queryWrapper.or(w -> {
                                w.eq(ClothingInfoEntity::getAffCode, entity.getCode());
                            });
                        })
                        .ne(ClothingInfoEntity::getId, entity.getId()));
        CoolPreconditions.check(check != null, "编号已存在已存在");

        if (entity.getCategory() == 2) {
            ClothingInfoEntity check1 = getOne(
                    QueryWrapper.create()
                            .and(queryWrapper -> {
                                queryWrapper.eq(ClothingInfoEntity::getCode, entity.getAffCode());
                                queryWrapper.or(w -> {
                                    w.eq(ClothingInfoEntity::getAffCode, entity.getAffCode());
                                });
                            })
                            .ne(ClothingInfoEntity::getId, entity.getId()));
            CoolPreconditions.check(check1 != null, "附属编号已存在已存在");
        } else {
            entity.setAffCode("");
        }
        /*if (entity.getCategory() == 4 || entity.getCategory() == 3) {
            entity.setSeason(0);
        }*/
        /**
         * 根据之前状态判断
         * 之前是0，3，现在是1：更新货架信息，
         * 之前是1，现在是0，3：更新货架信息，1：判断货架，不同则进行转库
         * 之前是2，现在是0，1，3：添加归还记录，1，更新货位信息，
         * 现在是2，之前不是2则进行借出操作，保存借出人员，如果之前是1，则更新货架
         */
        JSONObject tokenInfo = requestParams.getJSONObject("tokenInfo");
        BaseSysUserEntity sysUserEntity = baseSysUserService.getById(tokenInfo.get("userId").toString());
        Long opUserId = requestParams.getLong("opUserId");
        ClothingInfoEntity clothingInfo = getById(entity.getId());
        //当前被装正在操作，已外借
        CoolPreconditions.check(clothingInfo.getStatus() == 9, "当前被装正在操作");
        CoolPreconditions.check(clothingInfo.getStatus() == 4, "当前被装已外借");

        ClothingRecordEntity clothingRecord = new ClothingRecordEntity();
        clothingRecord.setCreateTime(new Date());
        clothingRecord.setClothingId(entity.getId());
        clothingRecord.setClothingCode(entity.getCode());
        clothingRecord.setSysUserId(sysUserEntity.getId());
        clothingRecord.setSysUserName(sysUserEntity.getName());
        //clothingRecord.setDepartmentId(entity.getDepartmentId());
        //之前是0，3，现在是1：更新货架信息
        if (clothingInfo.getStatus() == 0 || clothingInfo.getStatus() == 3) {
            if (entity.getStatus() == 1) {
                CargoEntity cargo = cargoMapper.selectOneById(entity.getCargoId());
                if (ObjectUtil.isNotEmpty(cargo)) {
                    clothingRecord.setCargoId(cargo.getId());
                    clothingRecord.setCargoPosition(cargo.getPosition());
                    clothingRecord.setType(5);
                    clothingRecord.setStatus(1);
                    clothingRecordService.save(clothingRecord);
                    //更新货架
                    cargo.setStatus(1);
                    cargoMapper.update(cargo);
                }
            }
        }
        //之前是1，现在是0，3：更新货架信息，1：判断货架，不同则进行转库
        if (clothingInfo.getStatus() == 1) {
            if (entity.getStatus() == 0 || entity.getStatus() == 3) {
                CargoEntity cargo = cargoMapper.selectOneById(clothingInfo.getCargoId());
                if (ObjectUtil.isNotEmpty(cargo)) {
                    clothingRecord.setCargoId(cargo.getId());
                    clothingRecord.setCargoPosition(cargo.getPosition());
                    clothingRecord.setType(6);
                    clothingRecord.setStatus(entity.getStatus() == 0 ? 1:0);
                    clothingRecordService.save(clothingRecord);
                    //更新货架状态
                    cargo.setStatus(0);
                    cargoMapper.update(cargo);
                }
            }
            if (entity.getStatus() == 1) {
                if (!clothingInfo.getCargoId().equals(entity.getCargoId())) {
                    //1-旧的，2-新的
                    CargoEntity cargo1 = cargoMapper.selectOneById(clothingInfo.getCargoId());
                    CargoEntity cargo2 = cargoMapper.selectOneById(entity.getCargoId());
                    if (ObjectUtil.isNotEmpty(cargo1)) {
                        ClothingRecordEntity clothingRecord1 = new ClothingRecordEntity();
                        clothingRecord1.setCreateTime(new Date());
                        clothingRecord1.setClothingId(entity.getId());
                        clothingRecord1.setClothingCode(entity.getCode());
                        clothingRecord1.setCargoId(cargo1.getId());
                        clothingRecord1.setCargoPosition(cargo1.getPosition());
                        clothingRecord1.setSysUserId(sysUserEntity.getId());
                        clothingRecord1.setSysUserName(sysUserEntity.getName());
                        //clothingRecord1.setDepartmentId(entity.getDepartmentId());
                        clothingRecord1.setType(3);
                        clothingRecord1.setStatus(1);
                        clothingRecordService.save(clothingRecord1);
                        //更新货位信息
                        cargo1.setStatus(0);
                        cargoMapper.update(cargo1);
                    }
                    ClothingRecordEntity clothingRecord2 = new ClothingRecordEntity();
                    clothingRecord2.setCreateTime(new Date());
                    clothingRecord2.setClothingId(entity.getId());
                    clothingRecord2.setClothingCode(entity.getCode());
                    clothingRecord2.setCargoId(cargo2.getId());
                    clothingRecord2.setCargoPosition(cargo2.getPosition());
                    clothingRecord2.setSysUserId(sysUserEntity.getId());
                    clothingRecord2.setSysUserName(sysUserEntity.getName());
                    //clothingRecord2.setDepartmentId(entity.getDepartmentId());
                    clothingRecord2.setType(4);
                    clothingRecord2.setStatus(1);
                    clothingRecordService.save(clothingRecord2);
                    //更新货位信息
                    cargo2.setStatus(1);
                    cargoMapper.update(cargo2);
                }
            }
        }

        //之前是2，现在是0，1，3：添加归还记录，1，更新货位信息，
        if (clothingInfo.getStatus() == 2) {
            if (entity.getStatus() == 0 || entity.getStatus() == 1 || entity.getStatus() == 3) {
                if (entity.getStatus() == 1) {
                    CargoEntity cargo = cargoMapper.selectOneById(entity.getCargoId());
                    if (ObjectUtil.isNotEmpty(cargo)) {
                        clothingRecord.setCargoId(cargo.getId());
                        clothingRecord.setCargoPosition(cargo.getPosition());
                        cargo.setStatus(1);
                        cargoMapper.update(cargo);
                    }
                }
                //归还人员
                AppUserEntity opUser = appUserService.getById(clothingInfo.getOpUserId());
                clothingRecord.setAppUserId(opUser.getId());
                clothingRecord.setType(2);
                clothingRecord.setStatus(entity.getStatus() == 3 ? 0:1);
                clothingRecordService.save(clothingRecord);
            }
        }
        //现在是2，之前不是2则进行借出操作，保存借出人员，如果之前是1，则更新货架
        if (entity.getStatus() == 2 && clothingInfo.getStatus() != 2) {
            CoolPreconditions.check(opUserId == null, "请选择借出人员");
            AppUserEntity appUser = appUserService.getById(opUserId);
            clothingRecord.setAppUserId(appUser.getId());
            if (clothingInfo.getStatus() == 1) {
                CargoEntity cargo = cargoMapper.selectOneById(clothingInfo.getCargoId());
                if (ObjectUtil.isNotEmpty(cargo)) {
                    clothingRecord.setCargoId(cargo.getId());
                    clothingRecord.setCargoPosition(cargo.getPosition());
                    cargo.setStatus(0);
                    cargoMapper.update(cargo);
                }

            }
            clothingRecord.setType(1);
            clothingRecord.setStatus(1);
            clothingRecordService.save(clothingRecord);
            //保存借出人员
            entity.setOpUserId(appUser.getId());
        }

        return super.update(requestParams, entity);
    }

    @Override
    public Object list(JSONObject requestParams, QueryWrapper queryWrapper) {
        Long departmentId = requestParams.getLong("departmentId");
        Integer category = requestParams.getInt("category");
        Integer season = requestParams.getInt("season");
        Long opUserId = requestParams.getLong("opUserId");
        Integer status = requestParams.getInt("status");
        queryWrapper.from(CLOTHING_INFO_ENTITY);
        if (departmentId != null) {
            queryWrapper.and(CLOTHING_INFO_ENTITY.DEPARTMENT_ID.eq(departmentId));
        }
        if (category != null) {
            queryWrapper.and(CLOTHING_INFO_ENTITY.CATEGORY.eq(category));
        }
        if (season != null) {
            queryWrapper.and(CLOTHING_INFO_ENTITY.SEASON.eq(season));
        }
        if (opUserId != null) {
            queryWrapper.and(CLOTHING_INFO_ENTITY.OP_USER_ID.eq(opUserId));
        }
        if (status != null) {
            queryWrapper.and(CLOTHING_INFO_ENTITY.STATUS.eq(status));
        }
        return mapper.selectListByQuery(queryWrapper);
    }

    @Override
    public ClothingInfoEntity getByCode(String code) {
        //ClothingInfoEntity clothingInfoEntity = getOne(QueryWrapper.create().eq(ClothingInfoEntity::getCode, code));
        ClothingInfoEntity clothingInfoEntity = getOne(QueryWrapper.create()
                .eq(ClothingInfoEntity::getCode, code)
                .or(w ->{
                    w.eq(ClothingInfoEntity::getAffCode, code);
                })
        );
        if (ObjectUtil.isNotEmpty(clothingInfoEntity) && clothingInfoEntity.getStatus() != 0) {
            CargoEntity cargoEntity = cargoMapper.selectOneById(clothingInfoEntity.getCargoId());
            if (ObjectUtil.isNotEmpty(cargoEntity)) {
                clothingInfoEntity.setCargoPosition(cargoEntity.getPosition());
            } else {
                clothingInfoEntity.setCargoPosition("暂无货架信息");
            }

        }
        return clothingInfoEntity;
    }

    @Override
    public List<ClothingInfoEntity> listByInfo(ClothingInfoEntity info) {
        QueryWrapper queryWrapper = new QueryWrapper();
        // 用户的部门权限
        Long[] permsDepartmentArr = info.getDepartmentIds();
        queryWrapper.select(CLOTHING_INFO_ENTITY.ALL_COLUMNS,
                CARGO_ENTITY.POSITION.as("cargoPosition"),
                CLOTHING_SPEC_ENTITY.NAME.as("specName"));
        queryWrapper.from(CLOTHING_INFO_ENTITY)
                .leftJoin(CARGO_ENTITY)
                .on(CLOTHING_INFO_ENTITY.CARGO_ID.eq(CARGO_ENTITY.ID))
                .leftJoin(CLOTHING_SPEC_ENTITY)
                .on(CLOTHING_INFO_ENTITY.SPEC_ID.eq(CLOTHING_SPEC_ENTITY.ID));
        if (permsDepartmentArr != null && permsDepartmentArr.length > 0) {
            queryWrapper.and(CLOTHING_INFO_ENTITY.DEPARTMENT_ID.in(
                    permsDepartmentArr,
                    !CoolSecurityUtil.getAdminUsername().equals("admin")));
            queryWrapper.groupBy(CLOTHING_INFO_ENTITY.ID);
        }
        if (StrUtil.isNotEmpty(info.getCode())) {
            queryWrapper.and(CLOTHING_INFO_ENTITY.CODE.eq(info.getCode())
                    .or(CLOTHING_INFO_ENTITY.AFF_CODE.eq(info.getCode())));
        }

        queryWrapper.and(CLOTHING_INFO_ENTITY.OP_USER_ID.eq(info.getOpUserId(), info.getOpUserId() != null));
        queryWrapper.and(CLOTHING_INFO_ENTITY.PR_USER_ID.eq(info.getPrUserId(), info.getPrUserId() != null));
        queryWrapper.and(CLOTHING_INFO_ENTITY.SPEC_ID.eq(info.getSpecId(), info.getSpecId() != null));
        queryWrapper.and(CLOTHING_INFO_ENTITY.CARGO_ID.eq(info.getCargoId(), info.getCargoId() != null));
        queryWrapper.and(CLOTHING_INFO_ENTITY.IS_PRIVATE.eq(info.getIsPrivate(), info.getIsPrivate() != null));
        queryWrapper.and(CLOTHING_INFO_ENTITY.CATEGORY.eq(info.getCategory(), info.getCategory() != null));
        //queryWrapper.and(CLOTHING_INFO_ENTITY.DEPARTMENT_ID.eq(info.getDepartmentId(), info.getDepartmentId() != null));
        queryWrapper.and(CLOTHING_INFO_ENTITY.STATUS.eq(info.getStatus(), info.getStatus() != null));
        if (info.getSeason() != null) {
            List<Integer> seasons = new ArrayList<>();
            seasons.add(info.getSeason());
            seasons.add(0);
            queryWrapper.and(CLOTHING_INFO_ENTITY.SEASON.in(seasons));
        }
        queryWrapper.and(CLOTHING_INFO_ENTITY.FACE_RECORD_ID.eq(info.getFaceRecordId(), info.getFaceRecordId() != null));
        queryWrapper.and(CLOTHING_INFO_ENTITY.ID.in(info.getIds(), info.getIds() != null && info.getIds().length > 0));
        queryWrapper.orderBy(CLOTHING_INFO_ENTITY.UPDATE_TIME.asc());
        return mapper.selectListByQuery(queryWrapper);
    }

    @Override
    public R addByCodeBatch(JSONObject requestParams) {
        String[] codeList = requestParams.getStr("codeList").split(",");
        Integer category = requestParams.getInt("category");
        Integer season = requestParams.getInt("season");
        Long specId = requestParams.getLong("specId");
        //Long departmentId = requestParams.getLong("departmentId");
        List<ClothingInfoEntity> list = new ArrayList<>();
        for (String code: codeList) {
            ClothingInfoEntity infoEntity = new ClothingInfoEntity();
            infoEntity.setCode(code);
            infoEntity.setIsPrivate(0);
            infoEntity.setCategory(category);
            if (category == 4 || category == 3) {
                infoEntity.setSeason(0);
            } else {
                infoEntity.setSeason(season);
            }
            infoEntity.setSpecId(specId);
            //infoEntity.setDepartmentId(departmentId);
            infoEntity.setStatus(0);
            list.add(infoEntity);
        }
        this.saveBatch(list);
        return R.ok();
    }

    @Override
    public void replaceSeason(Integer season) {
        BaseSysParamEntity baseSysParamEntity = sysParamService.getByKey("season");
        if (baseSysParamEntity == null) {
            //创建
            baseSysParamEntity = new BaseSysParamEntity();
            baseSysParamEntity.setKeyName("season");
            baseSysParamEntity.setName("被装季节");
            baseSysParamEntity.setData(season.toString());
            baseSysParamEntity.setDataType(0);
            baseSysParamEntity.setRemark("被装借出时的季节");
            sysParamService.add(baseSysParamEntity);
        } else {
            //修改
            baseSysParamEntity.setData(season.toString());
            sysParamService.updateById(baseSysParamEntity);
        }
    }

    @Override
    public void replaceIsBatch(Integer isBatch) {
        BaseSysParamEntity baseSysParamEntity = sysParamService.getByKey("clothingIsBatch");
        if (baseSysParamEntity == null) {
            //创建
            baseSysParamEntity = new BaseSysParamEntity();
            baseSysParamEntity.setKeyName("clothingIsBatch");
            baseSysParamEntity.setName("被装批量操作开关");
            baseSysParamEntity.setData(isBatch.toString());
            baseSysParamEntity.setDataType(0);
            baseSysParamEntity.setRemark("后台是否在批量操作被装");
            sysParamService.add(baseSysParamEntity);
        } else {
            //修改
            baseSysParamEntity.setData(isBatch.toString());
            sysParamService.updateById(baseSysParamEntity);
        }
    }

    @Override
    public R listByLendBatch(JSONObject requestParams) {
        List<ClothingBatchVO> batchVOList = requestParams.getBeanList("list", ClothingBatchVO.class);
        Integer season = requestParams.getInt("season");
        List<ClothingInfoEntity> clothingList = new ArrayList<>();
        for (ClothingBatchVO batchVO: batchVOList) {
            ClothingInfoEntity selectInfo = new ClothingInfoEntity();
            selectInfo.setStatus(1);
            selectInfo.setIsPrivate(0);
            selectInfo.setCategory(batchVO.getCategory());
            selectInfo.setSeason(season);
            selectInfo.setSpecId(batchVO.getSpecId());
            List<ClothingInfoEntity> list = listByInfo(selectInfo);
            if (list.size() < batchVO.getNum()) {
                batchVO.setNum(list.size());
            }
            for (int i = 0; i < batchVO.getNum(); i++) {
                list.get(i).setStatus(9);
                list.get(i).setFaceRecordId(0L);
                clothingList.add(list.get(i));
            }
            updateBatch(list);
        }
        return R.ok(clothingList);
    }

}